// pages/detail/detail.js
const { ledgerTools, ledgerConfig } = require('../../utils/ledgerTools')
const { formatTime, formatTimeTwo } = require("../../utils/util")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    nowDate: "",
    selectedDate: '',
    ledgerType: ledgerConfig().type,
    categoryList: ledgerConfig().category,
    monthlyIncome: 0,
    monthlyExpense: 0,
    ledgerData: [],
    monthBydayInfo: [],
  },

  /*切换日期 */
  pagechange: function () {
    const data = []
    const _self = this
    ledgerTools.getData().forEach(e => {
      const yearAndMonth = formatTimeTwo(formatTime(new Date(e.date)), "Y-M")
      if (data.indexOf(yearAndMonth) == -1) { data.push(yearAndMonth) }
    });
    wx.showActionSheet({ itemList: data, success: function (res) { _self.loadData(new Date(data[res.tapIndex])) } })
  },
  /*长按菜单 */
  Menu(e) {
    const _self = this
    wx.showActionSheet({
      itemList: ['修改', '删除'],
      success: function (res) {
        const tapI = res.tapIndex
        if (tapI == 1) {
          wx.showModal({
            title: '警告', content: '删除后无法恢复，确认删除?',
            success(res) {
              if (res.confirm) {
                ledgerTools.removeLedger(e.currentTarget.dataset.i)
                _self.loadData(selectedDate)
              } else if (res.cancel) { wx.showToast({ title: '已取消！', icon: "none" }) }
            }
          })
        } else if (tapI == 0) {
          getApp().globalData.updateId = e.currentTarget.dataset.i
          wx.switchTab({ url: '/pages/record/record' })
        }
      }
    })
  },

  /*加载数据 */
  loadData(yearAndMonth) {
    const dataByNowDate = ledgerTools.getDataByDate(formatTimeTwo(formatTime(yearAndMonth), "Y-M")) || []
    const MIC = dataByNowDate.reduce((x, y) => x + (y.type == this.data.ledgerType.indexOf("收入") ? y.money : 0), 0)
    const MEC = dataByNowDate.reduce((x, y) => x + (y.type == this.data.ledgerType.indexOf("支出") ? y.money : 0), 0)
    // monthBydayInfo: ledgerTools.getDataDateGroupByDay(formatTimeTwo(formatTime(new Date()), "Y-M"))
    this.setData({
      ledgerData: ledgerTools.getData(),
      selectedDate: yearAndMonth,
      // 'nowDate': formatTimeTwo(formatTime(new Date()), 'M月D日'),
      nowDate: formatTimeTwo(formatTime(yearAndMonth), "Y年M月"),
      monthlyIncome: MIC,
      monthlyExpense: MEC,
      monthBydayInfo: ledgerTools.getDataDateGroupByDay(formatTimeTwo(formatTime(yearAndMonth), "Y-M"))
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 0
      })
    }
    this.loadData(new Date())
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})